import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsOptional, IsString, ValidateNested } from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { BaseEntity } from "./base.entity";
import { Village } from "./village.entity";
import { file } from "./about/file";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class VillageBulletinBoard extends BaseEntity {

  @ApiProperty({
    description: '村告栏 -主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.villageBulletinBoard, {
    eager: true
  })
  village: Number | Village


  @ApiProperty({
    example: '标题',
    description: '标题',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  title: string

  @ApiProperty({
    example: '简介',
    description: '简介',
    required: true
  })
  @IsString({ always: true })
  @IsOptional()
  @Column({
    type: "text",
  })
  introduction: string
  
  @ApiProperty({
    example: '相关文件',
    description: '相关文件',
    required: true
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  relatedDocuments: file []
}